package com.example.dicos.controller;

import com.example.dicos.config.CommonResult;
import com.example.dicos.entity.dot.ManHourDto;
import com.example.dicos.entity.dot.ManHourUpdateDto;
import com.example.dicos.service.ManHourService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


@Api(tags = "工时模块")
@RestController
@RequestMapping("/manHour")
public class ManHourController extends BaseController {

    @Autowired
    private ManHourService manHourService;

    @ApiOperation("工时列表展示")
    @GetMapping("/queryManHourList")
    public CommonResult queryManHourList() {

        return CommonResult.success(manHourService.queryManHourList());
    }

    /**
     * 根据姓名查询
     *
     * @param userName
     * @return
     */
    @ApiOperation("根据姓名查询工时列表")
    @GetMapping("/queryByNameList")
    public CommonResult queryByNameList(String userName) {
        try {
            return CommonResult.success(manHourService.queryByNameList(userName));
        } catch (ParseException e) {
            return CommonResult.failed(e.getMessage());
        }
    }


    /**
     * 当月列表展示
     *
     * @return
     */
    @ApiOperation("查询当前用户工时列表(当月)")
    @GetMapping("/hour/queryUserByName")
    public CommonResult queryUserByName() {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String format = sdf.format(new Date());
            return CommonResult.success(manHourService.queryByName(super.getUserName(), format));
        } catch (ParseException e) {
            return CommonResult.failed(e.getMessage());
        }
    }


    @ApiOperation("根据用户名和日期筛选数据《日期格式 yyyy-MM 如：2022-07》")
    @GetMapping("/queryUserByNames")
    public CommonResult queryUserByNames(@RequestParam(value = "userName", defaultValue = "admin") @ApiParam(required = true, defaultValue = "admin") String userName,
                                         @RequestParam(value = "date", defaultValue = "2022-07") @ApiParam(required = true, defaultValue = "2022-07") String date) {
        try {
            return CommonResult.success(manHourService.queryByName(userName, date));
        } catch (ParseException e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    /**
     * 查询月份数据
     *
     * @param date
     * @return
     */
    @ApiOperation("根据日期查询对应月份的数据")
    @GetMapping("/queryDate")
    public CommonResult queryDate(String date) {

        return CommonResult.success(manHourService.queryDate(date));
    }

    @ApiOperation("添加员工工时信息")
    @PostMapping("/hour/addManHour")
    public CommonResult addManHour(@RequestBody ManHourDto manHour) {
        manHour.setUserId(super.getUserById());
        try {
            return CommonResult.success(manHourService.addManHour(manHour));
        } catch (ParseException e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation("修改员工工时信息")
    @PostMapping("/hour/updateManHour")
    public CommonResult updateManHour(@RequestBody ManHourUpdateDto manHourUpdate) {
        try {
            return CommonResult.success(manHourService.updateManHour(manHourUpdate));
        } catch (ParseException e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation("当前用户工时总和(当月)")
    @GetMapping("/hour/statisticsUserHour")
    public CommonResult statisticsUserHour() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String format = sdf.format(new Date());
        return CommonResult.success(manHourService.statisticsUserHour(super.getUserById(), format));
    }

    /**
     * 查看当月
     * @return
     */
    @ApiOperation("所有用户工时总和(查询当月)")
    @GetMapping("/statisticsHour")
    public CommonResult statisticsHour() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String format = sdf.format(new Date());
        return CommonResult.success(manHourService.statisticsHour(format));
    }



    /**
     * 在接口单独定义属性为Date，需要将类型转换，实体中注解解决即可
     * <p>
     * 定义全局需要实现接口  和下面代码一样
     * 创建CustomDate类实现WebBindingInitializer
     * implements WebBindingInitializer
     * @param binder
     * @param request
     */
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        //转换日期 注意这里的转化要和传进来的字符串的格式一直 如2015-9-9 就应该为yyyy-MM-dd
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
    }

    /**
     * 根据月份筛选整月
     * @param date
     * @return
     */
    @ApiOperation("用户工时总和(根据月份筛选)")
    @GetMapping("/statisticsHours")
    public CommonResult statisticsHours(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String format = sdf.format(date);
        return CommonResult.success(manHourService.statisticsHour(format));
    }


}
